package com.jdcjk.project.setting.view;

import android.graphics.drawable.AnimationDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.jdcjk.R;
import com.jdcjk.base.view.BaseActivity;
import com.jdcjk.project.setting.presenter.SettingPresenter;
import com.jdcjk.util.PreferencesUtils;
import com.jdcjk.view.SwitchView;

/**
 * Created by yu on 2016/12/16 0016.
 * 车辆其他设置界面
 */

public class OtherSettingActivity extends BaseActivity<SettingPresenter> implements View.OnClickListener, SwitchView.OnStateChangedListener {
    private final String TAG = OtherSettingActivity.class.getSimpleName();
    //顶栏标题textView
    private TextView barTitle_textview;

    //backButton
    private ImageButton back_button;
    //信息推送开关switchView
    private SwitchView pushInfo_switchView;
    //信息推送开关加载条
    private ImageView iLoadPush_imageView;
    //是否开启推送
    private boolean isPush;
    //声音开关switchView
    private SwitchView voice_switchView;
    //是否开启声音
    private boolean isVoice;
    //震动开关switchView
    private SwitchView vibration_switchview;
    //是否开启震动
    private boolean isVibration;
    //地图切换spinner
    private Spinner mapSwitch_spinner;
    //当前地图名称
    private String mapTypeName;

    @Override
    public int getLayoutId() {
        return R.layout.activity_other_setting;
    }

    @Override
    public void initContentView(Bundle savedInstanceState) {
        barTitle_textview = (TextView) findViewById(R.id.bar_title_textview);
        barTitle_textview.setText(R.string.setting);
        back_button = (ImageButton) findViewById(R.id.back_btn);
        back_button.setOnClickListener(this);

        pushInfo_switchView = (SwitchView) findViewById(R.id.push_info_switchview);
        pushInfo_switchView.setOnStateChangedListener(this);
        iLoadPush_imageView = (ImageView) findViewById(R.id.i_load_push);

        voice_switchView = (SwitchView) findViewById(R.id.voice_switchview);
        voice_switchView.setOnStateChangedListener(this);
        vibration_switchview = (SwitchView) findViewById(R.id.vibration_switchview);
        vibration_switchview.setOnStateChangedListener(this);
        mapSwitch_spinner = (Spinner) findViewById(R.id.map_switch_spinner);

        isPush = PreferencesUtils.getBoolean(this, PreferencesUtils.PUSH_INFO_SWITCH);
        isVoice = PreferencesUtils.getBoolean(this, PreferencesUtils.VOICE_SWITCH);
        isVibration = PreferencesUtils.getBoolean(this, PreferencesUtils.VIBRATION_SWITCH);
        mapTypeName = PreferencesUtils.getString(this, PreferencesUtils.MAP_TYPE);
        //设置默认显示
        pushInfo_switchView.toggleSwitch(isPush);
        voice_switchView.toggleSwitch(isVoice);
        vibration_switchview.toggleSwitch(isVibration);
        switch (mapTypeName) {
            case "GaoDe":
                mapSwitch_spinner.setSelection(0);
                break;
            case "BaiDu":
                mapSwitch_spinner.setSelection(1);
                break;
        }
        setSpinnerItemClick();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;
        }
    }

    @Override
    public void toggleToOn(SwitchView view) {
        switch (view.getId()) {
            case R.id.push_info_switchview:
                //信息推送开启监听
                LoadingTask loadingTask = new LoadingTask();
                loadingTask.execute(true);
                break;
            case R.id.voice_switchview:
                //声音开启监听
                isVoice = true;
                PreferencesUtils.setBoolean(OtherSettingActivity.this, PreferencesUtils.VOICE_SWITCH, true);
                voice_switchView.toggleSwitch(true);
                break;
            case R.id.vibration_switchview:
                //震动开启监听
                isVibration = true;
                PreferencesUtils.setBoolean(OtherSettingActivity.this, PreferencesUtils.VIBRATION_SWITCH, true);
                vibration_switchview.toggleSwitch(true);
                break;
        }
    }

    @Override
    public void toggleToOff(SwitchView view) {
        switch (view.getId()) {
            case R.id.push_info_switchview:
                //信息推送关闭监听
                LoadingTask loadingTask = new LoadingTask();
                loadingTask.execute(false);
                break;
            case R.id.voice_switchview:
                //声音关闭监听
                isVibration = false;
                PreferencesUtils.setBoolean(OtherSettingActivity.this, PreferencesUtils.VOICE_SWITCH, false);
                voice_switchView.toggleSwitch(false);
                //修改sharedPreferences里面设定对应的键值对
                break;
            case R.id.vibration_switchview:
                //夜间免打扰关闭监听
                isVibration = false;
                PreferencesUtils.setBoolean(OtherSettingActivity.this, PreferencesUtils.VIBRATION_SWITCH, false);
                vibration_switchview.toggleSwitch(false);
                //修改sharedPreferences里面设定对应的键值对
                break;
        }
    }

    private class LoadingTask extends AsyncTask<Boolean, Integer, Boolean> {

        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {
            //信息推送关闭加载条以及动画开启
            iLoadPush_imageView.setVisibility(View.VISIBLE);
            ((AnimationDrawable) iLoadPush_imageView.getBackground()).start();
        }

        //doInBackground方法内部执行后台任务,不可在此方法内修改UI
        @Override
        protected Boolean doInBackground(Boolean... params) {
            //获得输入参数（开启or关闭）
            Boolean isOpen = params[0];
            try {
                //休息500ms
                Thread.sleep(500);
                //修改sharedPreferences里面设定对应的键值对
                PreferencesUtils.setBoolean(OtherSettingActivity.this, PreferencesUtils.PUSH_INFO_SWITCH, isOpen);
                isPush = isOpen;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return isOpen;
        }

        //onPostExecute方法用于在执行完后台任务后更新UI,显示结果
        @Override
        protected void onPostExecute(Boolean s) {
            ((AnimationDrawable) iLoadPush_imageView.getBackground()).stop();
            iLoadPush_imageView.setVisibility(View.GONE);
            // 告诉SwitchView 更新UI
            Toast.makeText(getApplicationContext(), "推送服务开启完成", Toast.LENGTH_SHORT).show();
            pushInfo_switchView.toggleSwitch(s);
        }
    }

    //设置spinner item点击监听
    private void setSpinnerItemClick() {
        mapSwitch_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //修改显示的地图类型在SharedPreference中的值
                String[] reportTheWay = getResources().getStringArray(R.array.map_switch);
                String mapType = reportTheWay[position].equals("高德地图") ? "GaoDe" : "BaiDu";
                PreferencesUtils.setString(OtherSettingActivity.this, PreferencesUtils.MAP_TYPE, mapType);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

}
